package repository

import (
	"github.com/jinzhu/gorm"
	"mall-go/common"
	"mall-go/dto"
	"mall-go/model"
	"mall-go/vo"
)

type CarouselRepo struct {
	DB *gorm.DB
}

func NewCarouselRepo() CarouselRepo {
	db := common.GetDB()
	return CarouselRepo{DB: db}
}

// GetCarouselsForIndex 获取首页的五张轮播图，我偷懒了  不写vo了~~~
func (receiver CarouselRepo) GetCarouselsForIndex(number int) []model.Carousel {
	var carousels []model.Carousel
	receiver.DB.Where("is_deleted = 0").Order("carousel_rank desc").Limit(number).Find(&carousels)
	return carousels
}

// GetCarouselPage 获取轮播图分页数据
func (receiver CarouselRepo) GetCarouselPage(pageDTO dto.PageDTO) vo.CarouselPageResult {
	//找到该页的分页数据集合
	var list []model.Carousel
	receiver.DB.Where("is_deleted = 0").Order("carousel_rank desc").
		Limit(pageDTO.Limit).Offset(pageDTO.Start).Find(&list)
	//获取总的数量
	var total int
	receiver.DB.Model(&model.Carousel{}).Where("is_deleted = 0").Count(&total)
	pageResult := vo.CarouselPageResult{
		PageResult: vo.PageResult{
			TotalCount: total,
			PageSize:   pageDTO.Limit,
			TotalPage:  total / pageDTO.Limit,
			CurrPage:   pageDTO.Page,
		},
		List: list,
	}
	return pageResult
}
